package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private MemberService memberService;

    @Override
    @LcnTransaction
    public Order add(Map map) {
        Order order;
        String codeFromForm = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String orderDate = (String) map.get("orderDate");
        String setmealId = (String) map.get("setmealId");
        // 1 .  验证码校验
        String codeRedis = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER + telephone);

        if (StringUtils.isBlank(codeRedis)) {
            throw new RuntimeException("验证码过期！");
        }
        if (!codeRedis.equals(codeFromForm)) {
            throw new RuntimeException("验证码错误！");
        }
        //  2.  当天是否预约满   select count(1) from  t_ordersetting  where  number>reserviations and orderdate = ?
        int count = orderSettingService.isOkorNot(orderDate);
        if (count <= 0) {
            throw new RuntimeException("当天预约已满，请择日再约");
        }
        // 3 .  是否重复预约  -- 新会员还是老会员
        Member member = memberService.findMemberByTelephone(telephone);
        if (member != null) {
            //  老会员    根据member_id  setmealId   orderdate  判断是否重复预约
            int isNotExist = baseMapper.findOrderByMemberIdAndSetmealIdAndOrderDate(member.getId(), Integer.parseInt(setmealId), orderDate);
            if (isNotExist > 0) {
                throw new RuntimeException("请勿重复预约，择日预约");
            }
            // 预约 下单
            order = generateOrder(map, member);
        } else {
            // 新会员信息录入 t_member表中
            member = new Member();
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setSex((String) map.get("sex"));
            member.setName((String) map.get("name"));
            member.setRegTime(new Date());

            //  事务提交  主键才会诞生
            int mid = memberService.saveNumber(member);
            System.out.println("member = " + member);
            member.setId(mid);
            //  新会员  预约下单
            order = generateOrder(map, member);
        }
        //  添加订单记录  +  预约表 已经预约数+1
        orderSettingService.updateReservationsByOrderDate(orderDate);
        return order;
    }

    @Override
    public Map findOrderInfoById(Integer id) {
        Map map = baseMapper.findOrderInfoById(id);
        return map;
    }

    @LcnTransaction
    public Order generateOrder(Map map, Member member) {
        String orderType = (String) map.get("orderType");
        String setmealId = (String) map.get("setmealId");
        String orderDate = (String) map.get("orderDate");
        Order order = new Order();
        order.setOrderDate(DateUtils.parseString2Date(orderDate, "yyyy-MM-dd"));
        order.setSetmealId(Integer.valueOf(setmealId));
        order.setMemberId(member.getId());
        order.setOrderStatus("未到诊");
        order.setOrderType(orderType);
        save(order);
        return order;
    }
}
